package cn.kgc.vue.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.kgc.vue.anno.LogAnnotation;
import cn.kgc.vue.common.Operation;
import cn.kgc.vue.common.ResponseResult;
import cn.kgc.vue.common.ResponseStatus;
import cn.kgc.vue.dto.SchedulePageDTO;
import cn.kgc.vue.entity.Schedule;
import cn.kgc.vue.service.ScheduleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: 课工场
 * @since 2023/8/22
 * 开班计划控制层
 */
@RestController
@RequestMapping("schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    // 开班信息分页
    @GetMapping("page")
    public ResponseResult getPage(SchedulePageDTO schedulePageDTO) {
        return scheduleService.getPage(schedulePageDTO);
    }

    //更新和添加开班信息
    @PostMapping("saveOrUpdate")
    @LogAnnotation(model = "开班计划模块", operation = Operation.UPDATE)
    @RequiresPermissions(value = {"base:clazz:update", "base:clazz:add"}, logical = Logical.OR)
    public ResponseResult saveOrUpdate(@RequestBody Schedule schedule) {

        ResponseResult responseResult = new ResponseResult();

        LambdaQueryWrapper<Schedule> lambda = new QueryWrapper<Schedule>().lambda();
        lambda.eq(Schedule::getClassNo, schedule.getClassNo())
                .eq(ObjectUtil.isNotNull(schedule.getId()), Schedule::getLecturer, schedule.getLecturer())
                .eq(ObjectUtil.isNotNull(schedule.getId()), Schedule::getPrincipal, schedule.getPrincipal())
                .eq(ObjectUtil.isNotNull(schedule.getId()), Schedule::getClassTime, schedule.getClassTime());

        List<Schedule> list = scheduleService.list(lambda);
        if (list.size() > 0) {
            responseResult.setCode(500);
            responseResult.setMsg("开班计划重复");
            return responseResult;
        }
        scheduleService.saveOrUpdate(schedule);
        responseResult.success();
        return responseResult;
    }


    // 根据id 获取开班信息
    @GetMapping("getById")
    public ResponseResult getOne(Integer id) {
        ResponseResult responseResult = new ResponseResult();
        Schedule schedule = scheduleService.getById(id);
        responseResult.success(ResponseStatus.SUCCESS, schedule);
        return responseResult;
    }

    // 根据id 删除开班信息
    @DeleteMapping("delete")
    @LogAnnotation(model = "开班计划模块", operation = Operation.DELETE)
    @RequiresPermissions(value = {"base:clazz:delete"})
    public ResponseResult deleteOne(Integer id) {
        ResponseResult responseResult = new ResponseResult();
        scheduleService.removeById(id);
        responseResult.success();
        return responseResult;
    }
}
